﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WeiboSdk;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using WeiboSdk.PageViews;
using System.Threading;
using Hammock;
using Hammock.Web;


namespace WeiboSdkSample
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // 此处使用自己 AppKey 和 AppSecret，未经
            //审核的应用只支持用申请该Appkey的帐号来获取数据
            SdkData.AppKey = "";
            SdkData.AppSecret = "";

            // 您app设置的重定向页,必须一致
            SdkData.RedirectUri = "";

        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(SdkData.AppKey) || string.IsNullOrEmpty(SdkData.AppSecret)
                || string.IsNullOrEmpty(SdkData.RedirectUri))
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("请在中MainPage.xmal.cs的构造函数中设置自己的appkey、appkeysecret、RedirectUri.");
                });
                return;
            }
            AuthenticationView.OAuth2VerifyCompleted = (e1, e2, e3) => VerifyBack(e1, e2, e3);
            AuthenticationView.OBrowserCancelled = new EventHandler(cancleEvent);
            //其它通知事件...

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                NavigationService.Navigate(new Uri("/WeiboSdk;component/PageViews/AuthenticationView.xaml"
                    , UriKind.Relative));
            });
        

#if samplecode

            ClientOAuth.GetAccessToken("username", "password", (e1, e2, e3) =>
                {
                    if (true == e1)
                    {
                        Debug.WriteLine("accessToken:" + e3.accesssToken);
                        Debug.WriteLine("refleshToken:" + e3.refleshToken);
                        Debug.WriteLine("expriesIn:" + e3.expriesIn);
                    }
                    else
                    {
                        if (e2.errCode == SdkErrCode.NET_UNUSUAL)
                        {
                            Debug.WriteLine("测试");
                        }
                        else if (e2.errCode == SdkErrCode.SERVER_ERR)
                            Debug.WriteLine("服务器返回错误，错误码:" + e2.specificCode);
                    }
                });

            ClientOAuth.RefleshAccessToken("服务器获取的reflesh_token", (e1, e2, e3) =>
                {
                    if (true == e1)
                    {
                        Debug.WriteLine("accessToken:" + e3.accesssToken);
                        Debug.WriteLine("refleshToken:" + e3.refleshToken);
                        Debug.WriteLine("expriesIn:" + e3.expriesIn);
                    }
                    else
                    {
                        if (e2.errCode == SdkErrCode.NET_UNUSUAL)
                        {
                        }
                        else if (e2.errCode == SdkErrCode.SERVER_ERR)
                            Debug.WriteLine("服务器返回错误，错误码:" + e2.specificCode);
                    }
                });

            //cmdAtUsers data = new cmdAtUsers
            //{
            //    acessToken = "2.00jMcRMC4Ybz_E28ee9b843e6yO6TB",
            //    _keyword = "li"
            //};

            //SdkNetEngine net = new SdkNetEngine();
            //net.RequestCmd(SdkRequestType.AT_USERS, data, (e1, e2) =>
            //    {
            //        if (e2.errCode == SdkErrCode.SUCCESS)
            //        {
            //            Debug.WriteLine(e2.content);
            //        }
            //    });

            //cmdFriendShip data = new cmdFriendShip
            //{
            //    acessToken = "服务器获取的access_token",
            //    _sourceScreenName = "预备影帝",
            //    _screenName = "鲤鱼叔叔"
            //};

            //SdkNetEngine net = new SdkNetEngine();
            //net.RequestCmd(SdkRequestType.FRIENDSHIP_SHOW, data, (e1, e2) =>
            //{
            //    if (e2.errCode == SdkErrCode.SUCCESS)
            //    {
            //        Debug.WriteLine(e2.content);
            //    }
            //    else if(e2.errCode == SdkErrCode.NET_UNUSUAL)
            //    {
            //         Debug.WriteLine("网络错误");
            //    }
            //    else if(e2.errCode == SdkErrCode.SERVER_ERR)
            //    {
            //        Debug.WriteLine("服务器返回错误，错误码:" + e2.specificCode);
            //    }
            //});

            //Thread.Sleep(5000);

            //cmdFriendShip data2 = new cmdFriendShip
            //{
            //    acessToken = "2.00jMcRMC4Ybz_E28ee9b843e6yO6TB",
            //    _screenName = "蜂蜜柚子茶"
            //};
            //SdkNetEngine net2 = new SdkNetEngine();
            //net2.RequestCmd(SdkRequestType.FRIENDSHIP_SHOW, data2, (e1, e2) =>
            //{
            //    if (e2.errCode == SdkErrCode.SUCCESS)
            //    {
            //        Debug.WriteLine(e2.content);
            //    }
            //});


            //SdkCmdBase data = new SdkCmdBase
            //{
            //    acessToken = "2.00jMcRMC4Ybz_E28ee9b843e6yO6TB"
            //};

            //SdkNetEngine net2 = new SdkNetEngine();
            //RestRequest request = new RestRequest();
            //request.Method = WebMethod.Post;
            //request.Path = "/statuses/update.xml";
            //request.AddParameter("status", "吹灯，睡觉！");
            //net2.SendRequest(request, data, (e1) =>
            //    {
            //        if (e1.errCode == SdkErrCode.SUCCESS)
            //        {
            //            Debug.WriteLine(e1.content);
            //        }
            //        else if (e1.errCode == SdkErrCode.SERVER_ERR)
            //        {
            //            Debug.WriteLine("服务器返回错误，错误码:"+ e1.specificCode);
            //        }
            //        else if (e1.errCode == SdkErrCode.NET_UNUSUAL)
            //        {
            //            Debug.WriteLine("当前无网络");
            //        }
            //    });


            //SdkCmdBase data = new SdkCmdBase
            //{
            //    acessToken = "服务器返回的access_token",
            //};

            //SdkNetEngine net = new SdkNetEngine();
            //RestRequest request = new RestRequest();
            ////设置request
            //request.Method = WebMethod.Post;
            //request.Path = "/statuses/update.xml";
            //request.AddParameter("status", "吹灯，睡觉！");
            
            ////发送请求
            //net.SendRequest(request, data, (e1) =>
            //    {
            //        if (e1.errCode == SdkErrCode.SUCCESS)
            //        {
            //            Debug.WriteLine(e1.content);
            //        }
            //        else if (e1.errCode == SdkErrCode.SERVER_ERR)
            //        {
            //            Debug.WriteLine("服务器返回错误，错误码:" + e1.specificCode);
            //        }
            //        else if (e1.errCode == SdkErrCode.NET_UNUSUAL)
            //        {
            //            Debug.WriteLine("当前无网络");
            //        }
            //    });
           
#endif
        }

        private void VerifyBack(bool isSucess, SdkAuthError errCode, SdkAuth2Res response)
        {

            if (errCode.errCode == SdkErrCode.SUCCESS)
            {
                if (null != response)
                {
                    App.AccessToken = response.accesssToken;
                    App.RefleshToken = response.refleshToken;
                }

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri("/PageViews/SampleTimeline.xaml",
                        UriKind.Relative));
                });
            }
            else if (errCode.errCode == SdkErrCode.NET_UNUSUAL)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("检查网络");
                });
            }
            else if(errCode.errCode == SdkErrCode.SERVER_ERR)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("服务器返回错误，错误代码:" + errCode.specificCode);
                });
            }
            else
                Debug.WriteLine("Other Err.");

        }

#if samplecode
        void VerifyBack2(bool isSucess, SdkAuthError errCode, SdkAuthRes response)
        {

            if (errCode.errCode == SdkErrCode.SUCCESS)
            {
                if (null != response)
                {
                    App.AccessToken = response.acessToken;
                    App.AccessTokenSecret = response.acessTokenSecret;
                }

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri("/PageViews/SampleTimeline.xaml", UriKind.Relative));
                    homeButton.Visibility = Visibility.Visible;
                });
            }
            else if (errCode.errCode == SdkErrCode.NET_UNUSUAL)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("检查网络");
                });
            }
            else
            {
                Debug.WriteLine("Other Err.");
            }
        }
#endif
        private void cancleEvent(object sender, EventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                NavigationService.GoBack();
            });
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            //客户端方式获取，需要服务器授权
            //ClientOAuth2_0.GetAccessToken("UserName", "PassWord", (e1, e2) => loginBack(e1,e2));
        }

        private void homeButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageViews/SampleTimeline.xaml", UriKind.Relative));
        }

    }
}